package com.atguigu.service_edu.service.impl;

import com.atguigu.service_edu.client.VodClient;
import com.atguigu.service_edu.entity.*;
import com.atguigu.service_edu.entity.frontvo.CourseFrontVo;
import com.atguigu.service_edu.entity.frontvo.CourseWebVo;
import com.atguigu.service_edu.entity.vo.CourseInfoVo;
import com.atguigu.service_edu.entity.vo.EduCourseQuery;
import com.atguigu.service_edu.entity.vo.EduPublishVo;
import com.atguigu.service_edu.mapper.EduCourseMapper;
import com.atguigu.service_edu.service.*;
import com.atguigu.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author clark
 * @since 2020-04-10
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VodClient vodClient;


    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1.向课程表添加基本信息
        EduCourse eduCourse = null;
        if(courseInfoVo.getId() != null){
            eduCourse = baseMapper.selectById(courseInfoVo.getId());
            BeanUtils.copyProperties(courseInfoVo,eduCourse);
            baseMapper.updateById(eduCourse);
        }else{
            eduCourse = new EduCourse();
            BeanUtils.copyProperties(courseInfoVo,eduCourse);
            baseMapper.insert(eduCourse);
        }
        //2.向课程介绍表中添加课程信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescriptionService.saveOrUpdate(eduCourseDescription);
        return eduCourse.getId();

    }

    @Override
    public CourseInfoVo getCourseInfo(String id) {
        EduCourse eduCourse = baseMapper.selectById(id);
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(id);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    @Override
    public EduPublishVo getPublishInfo(String id) {

        EduPublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, EduCourseQuery educCourseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_modified");

        if(educCourseQuery == null){
            baseMapper.selectPage(pageParam,queryWrapper);
            return;
        }

        String subjectId = educCourseQuery.getSubjectId();
        String subjectParentId = educCourseQuery.getSubjectParentId();
        String title = educCourseQuery.getTitle();
        String teacherId = educCourseQuery.getTeacherId();

        if(!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("subject_id",subjectId);
            if(!StringUtils.isEmpty(subjectParentId)){
                queryWrapper.eq("subject_parent_id",subjectParentId);
            }
        }
        if(!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(teacherId)){
            queryWrapper.eq("teacher_id",teacherId);
        }

        baseMapper.selectPage(pageParam,queryWrapper);


    }

    @Override
    public void removeCourse(String courseId) {
        List<EduVideo> videos = eduVideoService.getVideosByCourseId(courseId);
        List<String> videosList = new ArrayList<>();
        for (EduVideo video : videos) {
            if(!StringUtils.isEmpty(video.getVideoSourceId())){
                videosList.add(video.getVideoSourceId());
            }
        }
        if(!videosList.isEmpty()){
            String videoIds = StringUtils.join(videosList,",");
            vodClient.deleteAlyiVideo(videoIds);
        }

        eduVideoService.removeByCourseId(courseId);
        eduChapterService.removeByCourseId(courseId);
        eduCourseDescriptionService.removeById(courseId);
        baseMapper.deleteById(courseId);

    }

    @Cacheable(value = "course",key = "'listLimit8'")
    @Override
    public List<EduCourse> listLimit8() {
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        eduCourseQueryWrapper.orderByDesc("id");
        eduCourseQueryWrapper.last("limit 8");
        List<EduCourse> eduCourses = baseMapper.selectList(eduCourseQueryWrapper);
        return eduCourses;
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageCourse,wrapper);
        List<EduCourse> records = pageCourse.getRecords();
        long pages = pageCourse.getPages();
        long current = pageCourse.getCurrent();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();
        boolean hasPrevious = pageCourse.hasPrevious();

        Map<String,Object> map = new HashMap<>();
        map.put("items",records);
        map.put("total",total);
        map.put("pages",pages);
        map.put("size",size);
        map.put("current",current);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {


        return baseMapper.getBaseCourseInfo(courseId);
    }


}
